<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:body>
	<ui:composition template="/pages/protected/admin/template.xhtml">
		<ui:define name='templatemo_content'>
			<!-- Cabeçalho  -->
			<h2>Alunos</h2>

			<!-- Lista -->
			<h:form id="listaAlunos">
				<p:dataTable id="tbAlunos" widgetVar="tbAlunos" var="aluno"
					value="#{matriculaMB.alunos}" paginator="true" rows="10"
					paginatorPosition="bottom"
					paginatorTemplate="{PageLinks} {RowsPerPageDropdown}"
					rowsPerPageTemplate="5,10,15"
					emptyMessage="Nenhum aluno foi encontrado.">

					<p:column headerText="Matricula" filterBy="#{aluno.matricula}"
						filterMatchMode="contains">
						<h:outputText value="#{aluno.matricula}" />
					</p:column>

					<p:column headerText="Nome" filterBy="#{aluno.nome}"
						filterMatchMode="contains">
						<h:outputText value="#{aluno.nome}" />
					</p:column>

					<p:column headerText="Componentes">
						<p:commandLink value=" #{fn:length(aluno.matriculas)}"
							type="button" update=":DialogHeader :dialogPanel"
							onclick="dlg.show()">
							<f:setPropertyActionListener value="#{aluno}"
								target="#{matriculaMB.selectedAluno}" />
						</p:commandLink>
					</p:column>
				</p:dataTable>
			</h:form>
			<p:dialog widgetVar="dlg" resizable="false" modal="true"
				dynamic="true" width=" 762px" height="420px">
				<p:ajax event="close" update=":salvarMatricula"
					listener="#{matriculaMB.dlgClose}" />
				<f:facet name="header">
					<h:outputText id="DialogHeader"
						value="Matriculas do aluno: #{matriculaMB.selectedAluno.nome}" />
				</f:facet>
				<p:growl id="messages" showDetail="true" />
				<p:outputPanel id="dialogPanel">
					<h:form id="salvarMatricula">
						
						<p>
						<p:commandButton value="Novo" icon="ui-icon-plus"
							update=":listaAlunos :salvarMatricula :messages :listaMatriculasAluno"
							actionListener="#{matriculaMB.doPrepararInserir}"
							disabled="#{matriculaMB.salvar}">
						</p:commandButton>
						<p:commandButton id="btnAlterar" icon="ui-icon-check"
							actionListener="#{matriculaMB.doSalvar}"
							update=":listaAlunos :salvarMatricula :messages :listaMatriculasAluno"
							disabled="#{not matriculaMB.salvar}" value="Salvar"
							oncomplete="javascript:setTimeout(function(){recarregar();},1000); function recarregar(){}">
						</p:commandButton>
						
						<p:commandButton value="Cancelar" icon="ui-icon-closethick"
							immediate="true" update=":listaAlunos :salvarMatricula :messages :listaMatriculasAluno" process="@this"
							disabled="#{not matriculaMB.salvar}">
							<f:setPropertyActionListener value="false"
								target="#{matriculaMB.salvar}" />
							<f:setPropertyActionListener value="0"
										target="#{matriculaMB.idSelectedComp}" />
						</p:commandButton>
						</p>
						<p:panelGrid id="matricula" columns="2">
							<h:outputLabel value="Curso: *" for="curso" />
							<p:selectOneMenu id="curso" style="width:300px;"
								value="#{matriculaMB.idSelectedComp}">
								<f:selectItem itemLabel="Selecione..." itemValue="" />
								<f:selectItems value="#{matriculaMB.componentes}" var="comp"
									itemValue="#{comp.id}" itemLabel="#{comp.nome}" />
							</p:selectOneMenu>
							<h:outputLabel for="situacao" value="Situacao: *" />
							<p:selectOneMenu id="situacao"
								value="#{matriculaMB.selectedMatricula.situacao}"
								required="true" label="Situacao">
								<f:selectItems value="#{matriculaMB.situacoes}" />
							</p:selectOneMenu>
						</p:panelGrid>
					</h:form>
					<br />
					<!-- Lista de Matriculas -->
					<h:form id="listaMatriculasAluno">
						<p:dataTable id="tbMatriculas" widgetVar="tbMatriculas" var="mat"
							value="#{matriculaMB.selectedAluno.matriculas.toArray()}"
							paginator="true" rows="5" paginatorPosition="bottom"
							paginatorTemplate="{PageLinks} {RowsPerPageDropdown}"
							emptyMessage="Nenhuma matrícula foi encontrada para este aluno."
							>

							<p:column headerText="Curso" filterBy="#{mat.cc.nome}"
								filterMatchMode="contains">
								<h:outputText value="#{mat.cc.nome}" />
							</p:column>

							<p:column headerText="Status" filterBy="#{mat.situacao}"
								filterMatchMode="contains">
								<h:outputText value="#{mat.situacao}" />
							</p:column>

							<p:column style="width:6%">
								<p:commandButton id="btnEditar" icon="ui-icon-pencil"
									update=":listaAlunos :salvarMatricula :messages :listaMatriculasAluno"
									disabled="#{matriculaMB.salvar}">
									<f:setPropertyActionListener value="#{mat}"
										target="#{matriculaMB.selectedMatricula}" />
									<f:setPropertyActionListener value="#{mat.cc.id}"
										target="#{matriculaMB.idSelectedComp}" />
								</p:commandButton>
								<p:tooltip for="btnEditar" value="Deletar" showEffect="fade"
									hideEffect="fade" />
								<p:commandButton id="btnDeletar" icon="ui-icon-trash"
									update=":listaAlunos :salvarMatricula :messages :listaMatriculasAluno"
									disabled="#{matriculaMB.salvar}"
									action="#{matriculaMB.doRemover(mat)}"
									>
								</p:commandButton>
								<p:tooltip for="btnDeletar" value="Deletar" showEffect="fade"
									hideEffect="fade" />
							</p:column>
						</p:dataTable>
					</h:form>

				</p:outputPanel>
			</p:dialog>
		</ui:define>
	</ui:composition>
</h:body>
</html>